#include "TreeItem.h"
#include <stdio.h>
CTreeItem::CTreeItem(QTreeWidgetItem *TreeItem)
{
    m_CurrentItem=TreeItem;
    m_Index=0;
}

CTreeItem::~CTreeItem(void)
{
}
QTreeWidgetItem* CTreeItem::GetChild()
{
    QTreeWidgetItem *tmp=m_CurrentItem->child(0);
    if (tmp==NULL)
    {
        printf("GetChild NULL\n");
        return NULL;
    }
    m_Index=m_CurrentItem->indexOfChild(tmp);
    m_CurrentItem=tmp;
    //printf("GetChild:%s\n",m_CurrentItem->text(0).toStdString().c_str());
    return tmp;
}
QTreeWidgetItem* CTreeItem::GetParent()
{
    if (m_CurrentItem->parent()!=NULL)
    {
        QTreeWidgetItem *parent=m_CurrentItem->parent();
        
        QTreeWidgetItem *root=parent->parent();
        if (root==NULL)
        {
            m_Index=parent->indexOfChild(m_CurrentItem);
        }
        else
        {
            m_Index=root->indexOfChild(parent);
            //printf("GetParent:%s\n",parent->text(0).toStdString().c_str());
            m_CurrentItem=parent;
        }
        return parent;
    }
    else
    {
        printf("GetParent NULL\n");
        return NULL;
    }
    
}
QTreeWidgetItem* CTreeItem::GetNext()
{
    QTreeWidgetItem *parent=m_CurrentItem->parent();
    if (parent==NULL)
    {
        printf("get next NULL\n");
        return NULL;
    }
    
    m_Index++;
    if (m_Index > parent->childCount())
    {
        m_Index--;
        printf("get next error\n");
        return NULL;
    }
    else
    {
        if (parent->child(m_Index)!=NULL)
        {
            m_CurrentItem=parent->child(m_Index);
            //printf("GetNextPP:%s\n",parent->text(0).toStdString().c_str());
            //printf("GetNext:%d %s\n",m_Index,m_CurrentItem->text(0).toStdString().c_str());
        }
        else
        {
            m_Index--;
        }
        return m_CurrentItem;
    }
}
QTreeWidgetItem* CTreeItem::GetCurrentItem()
{
    //printf("Current:%s\n",m_CurrentItem->text(0).toStdString().c_str());
    return m_CurrentItem;
}
